<?php
define('_file_','edit_article');
if ( !isGod() )
	if ( !getRights($mod,'full') )
		if ( !News_getAccessFile(_file_) )
		{
			showMsg('Không có quyền truy cập vào trang này này !','ex','center');
			die();
		}
	$tid = getVar('hidTid',true,true);
	$aid = getVar('hidAid',true,true);
	$imgidold = getVar('hidPicture',true,true);

	$title = getVar('txtTitle',false,true);
	$scontent = getVar('txtSContent',false,true);
	$fcontent = getVar('txtFContent',false,true);
	
	$title1 = getVar('txtTitle1',false,true);
	$scontent1 = getVar('txtSContent1',false,true);
	$fcontent1 = getVar('txtFContent1',false,true);
	
	$datepost = getVar('txtDatePost',false,true);
	$isDelete = getVar('chkDeletePicture',false,true);
	$username = $_SESSION['sesusername'];
	
	$datepost = date('Y-m-d');

	$imgid = 0;

	/*******************************************************************************/	
	//neu co anh -> insert vao db anh
	//qui trinh: upload anh len thu muc tam tren server -> ghi vao db -> xoa anh trong thu muc tam
	$upload_dir = 'temp/';

	$max_file_upload=return_bytes(ini_get('post_max_size'));

	if($_FILES['filPicture']['size'] > 0 )
	{
		$upload_file=$upload_dir.basename($_FILES['filPicture']['name']);

		if(file_exists($_FILES['filPicture']['name']))
		{
			$temp=explode(basename($_FILES['filPicture']['name']));
			$temp[0].=substr(rand(0,1000),0,2);	
		}

		$extension=strtolower(strrchr($upload_file,"."));

		if(empty($arr_extension[$extension]))
			die('KhÃ´ng Ä‘Ãºng pháº§n má»Ÿ rá»™ng !');

		if(filesize($_FILES['filPicture']['tmp_name'])< $max_file_upload)
		{	
			if(is_uploaded_file($_FILES['filPicture']['tmp_name']))
			{
				if( !move_uploaded_file($_FILES['filPicture']['tmp_name'],$upload_file) )
					die('Upload khÃ´ng thÃ nh !');
				else
				{
					$imgid = InsertImgToDB('temp/',basename($_FILES['filPicture']['name']));
					unlink($upload_file);
				}
			}
			else 
				die('Không thể chép vào thư mục tạm được !');
		}
		else
			die('Kích thước file quá lớn !');
		
		//co cap nhat lai anh
		//tim xem co anh cu hay kg ? -> co -> xoa anh cu
		$sql = "DELETE FROM images WHERE imgid=$imgidold";
		$link->execute($sql);
		//insert anh moi
		$sql = "UPDATE article SET title_vn='$title',title_en='$title1',short_content_vn='$scontent',short_content_en='$scontent1',full_content_vn='$fcontent',full_content_en='$fcontent1',picture=$imgid,datepost='$datepost',username='$username' WHERE autoid=$aid AND tid=$tid";
	}
	else
	{
		//khong co cap nhat anh moi -> giu lai anh cu
		$sql = "UPDATE article SET title_vn='$title',short_content_vn='$scontent',full_content_vn='$fcontent',title_en='$title1',short_content_en='$scontent1',full_content_en='$fcontent1',datepost='$datepost',username='$username' WHERE autoid=$aid AND tid=$tid";
	}

	if ( $isDelete != '' )	//xoa bo anh
	{
		$sql = "UPDATE article SET picture=0 WHERE autoid=$aid AND tid=$tid";
		$link->execute($sql);

		//xoa trong db
		$sql = "DELETE FROM images WHERE imgid=$imgidold";
		$link->execute($sql);
	}

	/*******************************************************************************/

	$link->execute($sql);
		
	header("Location: admin.php?mod=$mod&act=list_article&tid=$tid");
	die();
?>